﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace PMS.Utils
{
    public static class StringExtends
    {
        /// <summary>
        /// string转换成int32
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static int ToInt32(string s)
        {
            return int.TryParse(s, out _) ? int.Parse(s) : 0;
        }

        /// <summary>
        /// object 转换成int32
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public static int ToInt32(this object o)
        {
            return o != null ? int.TryParse(o.ToString(), out int _) ? Convert.ToInt32(o) : 0 : 0;
        }

        /// <summary>
        /// 转Double
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static double ToDouble(string s)
        {
            return double.TryParse(s, out _) ? double.Parse(s) : 0;
        }

        /// <summary>
        /// 转Float
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static float ToFloat(string s)
        {
            return float.TryParse(s, out _) ? float.Parse(s) : 0;
        }

        /// <summary>
        /// 转GUID成功返回GUID失败返回NULL
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static Guid? ToGuid(string s)
        {
            if (Guid.TryParse(s, out _))
            {
                return Guid.Parse(s);
            }
            return null;
        }

        public static DateTime ToDateTime(this string s)
        {
            return DateTime.TryParse(s, out _) ? Convert.ToDateTime(s) : new DateTime(1900, 1, 1);
        }

        /// <summary>
        /// 转List集合
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="s"></param>
        /// <returns></returns>
        public static List<T> ToGeneric<T>(this string s)
        {
            return JsonConvert.DeserializeObject<List<T>>(s);
        }

        /// <summary>
        /// string 转换成 Asc 或 Desc  默认转换成 Desc
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string ToOrderSort(this string s)
        {
            return s.ToLower() == "asc".ToLower() ? "asc" : "desc";
        }


        public static string ToDapperLike(this string s)
        {
            return "%" + s + "%";
        }
    }
}
